<?php

namespace app\admin\controller;

class AreaBase
{
    public function index()
    {
        $query = \app\model\Area::field(['id', 'name', 'sort'])->order('sort', 'asc');

        if ($name = input('name')) {
            $query->whereLike('name', '%' . $name . '%');
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['areas' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Area::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Area::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $model = \app\model\Area::create(input());

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $model = \app\model\Area::field(['id', 'name'])->where('id', input('id'))->find();

        return json(['code' => 20000, 'data' => ['area' => $model]]);
    }
}
